﻿using System.Globalization;

namespace ConsoleApp;

public class DemoDateTime
{

    /// <summary>
    /// 当前时间
    /// </summary>
    //public  static DateTime dt = DateTime.Now; //当前时间 

    //DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") //24小时制
    //DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")  //12小时制 

    /// <summary>
    /// 今天开始,0时0分0秒：
    /// </summary>
    public static DateTime startCurrentDay = Convert.ToDateTime(DateTime.Now.ToString("D"));

    /// <summary>
    /// 今天结束,23时59分59秒：
    /// </summary>
    //public static DateTime endCurrentDay = Convert.ToDateTime(DateTime.Now.AddDays(1).ToString("D").ToString()).AddSeconds(-1);
    //public static DateTime endCurrentDay = DateTime.Now.Date.AddDays(1).AddSeconds(-1);
    public static DateTime endCurrentDay = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);

    /// <summary>
    /// 昨天开始
    /// </summary>
    public static DateTime startLastDay = DateTime.Now.AddDays(-1).Date;

    /// <summary>
    /// 昨天结束
    /// </summary>
    public static DateTime endLastDay = startLastDay.AddDays(1).AddSeconds(-1);


    /// <summary>
    /// 今天星期几
    /// </summary>
    public static int currentDayOfWeek = Convert.ToInt32(DateTime.Now.DayOfWeek.ToString("d"));

    /// <summary>
    /// 本周周一
    /// </summary>
    public static DateTime startCurrentWeek = DateTime.Now.AddDays(1 - ((currentDayOfWeek == 0) ? 7 : currentDayOfWeek)).Date;

    /// <summary>
    /// 本周周日
    /// </summary>
    public static DateTime endCurrentWeek = startCurrentWeek.AddDays(7).AddSeconds(-1);

    /// <summary>
    /// 上周周一
    /// </summary>
    public static DateTime startLastWeek = startCurrentWeek.AddDays(-7).Date;

    /// <summary>
    /// 上周周日
    /// </summary>
    public static DateTime endLastWeek = startLastWeek.AddDays(7).AddSeconds(-1);

    /// <summary>
    /// 本月月初
    /// </summary>
    public static DateTime startCurrentMonth = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date; //本月月初 

    /// <summary>
    /// 本月月末
    /// </summary>
    public static DateTime endCurrentMonth = DateTime.Now.AddDays(1 - DateTime.Now.Day).Date.AddMonths(1).AddSeconds(-1); //本月月末  //DateTime endMonth = startMonth.AddDays((dt.AddMonths(1) - dt).Days - 1);  //本月月末   

    /// <summary>
    /// 上月月初
    /// </summary>
    public static DateTime startLastMonth = DateTime.Now.AddMonths(-1).Date.AddDays(1 - DateTime.Now.Day);

    /// <summary>
    /// 上月月末
    /// </summary>
    public static DateTime endLastMonth = DateTime.Parse(DateTime.Now.AddDays(1 - DateTime.Now.Day).ToShortDateString()).AddSeconds(-1);

    /// <summary>
    /// 本季度初
    /// </summary>
    public static DateTime startCurrentQuarter = DateTime.Now.AddMonths(0 - (DateTime.Now.Month - 1) % 3).AddDays(1 - DateTime.Now.Day).Date; //本季度初 

    /// <summary>
    /// 本季度末
    /// </summary>
    public static DateTime endCurrentQuarter = startCurrentQuarter.AddMonths(3).AddSeconds(-1); //本季度末  

    /// <summary>
    /// 上季度初
    /// </summary>
    public static DateTime startLastQuarter = startCurrentQuarter.AddMonths(-3).Date;

    /// <summary>
    /// 上季度末
    /// </summary>
    public static DateTime endLastQuarter = startLastQuarter.AddMonths(3).AddSeconds(-1);

    /// <summary>
    /// 今年年初
    /// </summary>
    public static DateTime startCurrentYear = new DateTime(DateTime.Now.Year, 1, 1).Date; //本年年初

    /// <summary>
    /// 今年年末
    /// </summary>
    public static DateTime endCurrentYear = new DateTime(DateTime.Now.Year, 12, 31, 23, 59, 59); //本年年末

    /// <summary>
    /// 去年年初
    /// </summary>
    public static DateTime startLastYear = startCurrentYear.AddYears(-1).Date;

    /// <summary>
    /// 去年年末
    /// </summary>
    public static DateTime endLastYear = endCurrentYear.AddYears(-1);

    //至于昨天、明天、上周、上月、上季度、上年度等等，只要AddDays()、AddMonths()、AddYears()这几种方法组合一下就可以了。

        public static string Format(DateTime dateTime, string format = "yyyyMMddHHmmss")
        {
            return dateTime.ToString(format);
        }
        
        public static string GetDate(string format = "yyyy-MM-dd")
        {
            return DateTime.Now.ToString(format);
        }
        
        public static string GetLastDate(int day = -1, string format = "yyyy-MM-dd")
        {
            return DateTime.Now.AddDays(day).ToString(format);
        }
        
        public static string GetDateTime(string format = "yyyy-MM-dd HH:mm:ss")
        {
            return DateTime.Now.ToString(format);
        }

        public static DateTime Parse(string s, string format = "yyyyMMddHHmmss")
        {
            return DateTime.ParseExact(s, format, CultureInfo.InvariantCulture);
        }

        /// <summary>
        /// 字符串转日期时间字符串
        /// </summary>
        /// <param name="date">20240127</param>
        /// <param name="time">092310</param>
        /// <returns></returns>
        public static string ParseToString(string date, string time)
        {
            return ParseDate(date) + " " + ParseTime(time);
        }

        public static string ParseDate(string s, string inFormat = "yyyyMMdd", string outFormat = "yyyy-MM-dd")
        {
            return DateTime.ParseExact(s, inFormat, null).ToString(outFormat);
        }

        public static string ParseTime(string s, string inFormat = "HHmmss", string outFormat = "HH:mm:ss")
        {
            return DateTime.ParseExact(s, inFormat, null).ToString(outFormat);
        }
    
    
    /// <summary>
    /// 比较时间大小
    /// </summary>
    public static void Compare()
    {
        // 1. 使用比较运算符比较日期

        var st1 = "12:13";
        var st2 = "14:14";
        var dt1 = Convert.ToDateTime(st1);
        var dt2 = Convert.ToDateTime(st2);
        var dt3 = DateTime.Now;

        // 2.使用 CompareTo 方法比较日期
        if (DateTime.Compare(dt1, dt2) > 0)
            Console.WriteLine(st1 + ">" + st2);
        else
            Console.WriteLine(st1 + "<" + st2);

        if (DateTime.Compare(dt1, dt3) > 0)
            Console.WriteLine(st1 + ">" + dt3);
        else
            Console.WriteLine(st1 + "<" + dt3);
    }

    /// <summary>
    /// 计算两个日期的时间间隔
    /// </summary>
    /// <param name="dateTime1"></param>
    /// <param name="dateTime2"></param>
    /// <returns></returns>
    public string DateDiff(DateTime dateTime1, DateTime dateTime2)
    {
        var ts1 = new TimeSpan(dateTime1.Ticks);
        var ts2 = new TimeSpan(dateTime2.Ticks);
        var ts = ts1.Subtract(ts2).Duration();
        return ts.Days + "天" + ts.Hours + "小时" + ts.Minutes + "分钟" + ts.Seconds + "秒";
    }

    // 得到某年某月的天数
    public static int GetDaysInMonth(int rYear, int rMonth)
    {
        var dt1 = DateTime.Parse(rYear + "-" + rMonth + "-01");
        var dt2 = dt1.AddMonths(1);
        var ts = dt2 - dt1;
        return (int)ts.TotalDays;
    }

    /// <summary>
    /// 获取季度
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static int Quarter(DateTime date)
    {
        return (date.Month + 2) / 3;
    }
    
    //得到星期Text
    public static string GetDayOfWeekHtml(int rDayOfWeek)
    {
        switch (rDayOfWeek)
        {
            case (int)DayOfWeek.Sunday:
                return "<font color=\"#ff0000\">星期日</font>";
            case (int)DayOfWeek.Monday:
                return "<font color=\"#000000\">星期一</font>";
            case (int)DayOfWeek.Tuesday:
                return "<font color=\"#000000\">星期二</font>";
            case (int)DayOfWeek.Wednesday:
                return "<font color=\"#000000\">星期三</font>";
            case (int)DayOfWeek.Thursday:
                return "<font color=\"#000000\">星期四</font>";
            case (int)DayOfWeek.Friday:
                return "<font color=\"#000000\">星期五</font>";
            case (int)DayOfWeek.Saturday:
                return "<font color=\"#008800\">星期六</font>";
            default:
                return "";
        }
    }

    public static string GetDayOfWeek()
    {
        var today = DateTime.Now;
        var dayOfWeek = today.DayOfWeek;

        var message = "";
        switch (dayOfWeek)
        {
            case DayOfWeek.Sunday:
                message = "星期日";
                break;
            case DayOfWeek.Monday:
                message = "星期一";
                break;
            case DayOfWeek.Tuesday:
                message = "星期二";
                break;
            case DayOfWeek.Wednesday:
                message = "星期三";
                break;
            case DayOfWeek.Thursday:
                message = "星期四";
                break;
            case DayOfWeek.Friday:
                message = "星期五";
                break;
            case DayOfWeek.Saturday:
                message = "星期六";
                break;
            default:
                message = "未知";
                break;
        }
        return message;
    }

    // 得到某年某月的起止日期，格式为0000-00-00
    public static string[] GetBeginEndDate(int rYear, int rMonth)
    {
        var arr = new string[2];
        var dt1 = DateTime.Parse(rYear + "-" + rMonth + "-01");
        arr[0] = dt1.ToLongDateString();
        var dt2 = dt1.AddMonths(1).AddDays(-1);
        arr[1] = dt2.ToLongDateString();
        return arr;
    }

    public static DateTime getDate(int day)
    {
        var today = DateTime.Now;
        return today.Subtract(TimeSpan.FromDays(day));
    }
    
    /// <summary>
    /// 获取两个日期之间的所有季度
    /// </summary>
    /// <param name="startDate"></param>
    /// <param name="endDate"></param>
    /// <returns></returns>
    public static List<DateTime> GetQuartersBetweenDates(DateTime startDate, DateTime endDate)
    {
        var quarters = new List<DateTime>();
        var startYear = startDate.Year;
        var endYear = endDate.Year;

        for (var year = startYear; year <= endYear; year++)
        {
            for (var quarter = 1; quarter <= 4; quarter++)
            {
                var quarterDate = new DateTime(year, quarter * 3 - 2, 1);
                if (quarterDate >= startDate && quarterDate <= endDate)
                {
                    quarters.Add(quarterDate);
                }
            }
        }

        return quarters;
    }
}